package com.hmsm.sys.daoImpl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import com.hmsm.server.base.BaseDao;
import com.hmsm.sys.dao.DynamicCipherCardDao;
import com.hmsm.sys.po.DynamicCipherCard;

@Repository
public class DynamicCipherCardDaoImpl extends BaseDao<DynamicCipherCard, Integer> implements
		DynamicCipherCardDao {
	
	/**
	 *  @Description:获取所有未绑定的SN信息
	 *  @ClassName  :com.hmsm.sys.daoImpl.DynamicCipherCardDaoImpl.java
	 *  @Author     :yaosq
	 *  @CreateDate :2013-11-15 下午12:03:50  
	 **/
	public List<DynamicCipherCard> getDynamicInfo() {
		
		String sql="SELECT * FROM dynamic_cipher_card where IS_BINDING = 0"; 
		List<DynamicCipherCard> list = null;
		list= this.queryBySql(sql);
		return list;
	}
	
	 /**
	 *  @Description:记录条数
	 *  @MethodName :getDynamicInfoCount
	 *  @Author     :fengt
	 *  @CreateDate :2013-11-19 下午02:07:27 
	 *  @return
	 **/
	public int getDynamicInfoCount(final int snCode, final int isBinding){
		Object obj = getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria cri = session.createCriteria(DynamicCipherCard.class);
				if(snCode > 0){
					cri.add(Restrictions.eq("snCode", snCode));
				}
				if(isBinding >= 0){
					cri.add(Restrictions.eq("isBinding", isBinding));
				}
				return cri.setProjection(Projections.count("pkId")).uniqueResult();
			}
		});
		return Integer.valueOf(obj.toString());
	}
	
	/**
	 *  @Description:
	 *  @MethodName :getDynamicInfoList
	 *  @Author     :fengt
	 *  @CreateDate :2013-11-19 下午02:08:58 
	 *  @param snCode
	 *  @param isBinding
	 *  @return
	 **/
	@SuppressWarnings("unchecked")
	public List<DynamicCipherCard> getDynamicInfoList(final Integer index, final Integer maxresult, final int snCode, final int isBinding){
		Object obj = getHibernateTemplate().execute(new HibernateCallback() {
			
			@Override
			public Object doInHibernate(Session session) throws HibernateException,
					SQLException {
				Criteria cri = session.createCriteria(DynamicCipherCard.class);
				if(snCode > 0){
					cri.add(Restrictions.eq("snCode", snCode));
				}
				if(isBinding >= 0){
					cri.add(Restrictions.eq("isBinding", isBinding));
				}
				return cri.setFirstResult((index - 1) * maxresult).setMaxResults(maxresult).list();
			}
		});
		return (List<DynamicCipherCard>)obj;
	}

	/**
	  * 
	  *  @Description:修改sn密令信息分配标志
	  *  @MethodName :updateSNInfo
	  *  @Author     :yaosq
	  *  @CreateDate :2013-11-15 下午04:30:20 
	  *  @param sn
	  *
	  */
	 public void updateSNInfo(Integer sn){
		 String sql="SELECT * FROM dynamic_cipher_card where SN_CODE ="+sn; 
		 List<DynamicCipherCard> list = null;
			list= this.queryBySql(sql);
			if(list.size() > 0){
				DynamicCipherCard dynamicCipherCard = new DynamicCipherCard();
				for(int i = 0;i<list.size();i++){
					dynamicCipherCard = list.get(i);
					dynamicCipherCard.setIsBinding(1);
				}
				this.update(dynamicCipherCard);
			}
	 }
	 
	 /**
	  * 
	  *  @Description:解除绑定
	  *  @MethodName :jcSNInfo
	  *  @Author     :yaosq
	  *  @CreateDate :2013-11-15 下午06:35:42 
	  *  @param sn
	  *
	  */
	 public void jcSNInfo(Integer sn){
		 String sql="SELECT * FROM dynamic_cipher_card where SN_CODE ="+sn; 
		 List<DynamicCipherCard> list = null;
			list= this.queryBySql(sql);
			if(list.size() > 0){
				DynamicCipherCard dynamicCipherCard = new DynamicCipherCard();
				for(int i = 0;i<list.size();i++){
					dynamicCipherCard = list.get(i);
					dynamicCipherCard.setIsBinding(0);
				}
				this.update(dynamicCipherCard);
			}
		 
	 }
}
